package mileena

class AppPlaceController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	
	def searchAJAX = {
		def places = AppPlace.findAllByAppPlaceNameLike("%${params.query}%")

		//Create XML response
		render(contentType: "text/xml") {
		results() {
			places.each { place ->
			result(){
				name(place.appPlaceName)
						//Optional id which will be available in onItemSelect
						id(place.id)
			}
		}
			}
		}
	}

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [appPlaceInstanceList: AppPlace.list(params), appPlaceInstanceTotal: AppPlace.count()]
    }

    def create = {
        def appPlaceInstance = new AppPlace()
        appPlaceInstance.properties = params
        return [appPlaceInstance: appPlaceInstance]
    }

    def save = {
        def appPlaceInstance = new AppPlace(params)
        if (appPlaceInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), appPlaceInstance.id])}"
            redirect(action: "show", id: appPlaceInstance.id)
        }
        else {
            render(view: "create", model: [appPlaceInstance: appPlaceInstance])
        }
    }

    def show = {
        def appPlaceInstance = AppPlace.get(params.id)
        if (!appPlaceInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), params.id])}"
            redirect(action: "list")
        }
        else {
            [appPlaceInstance: appPlaceInstance]
        }
    }

    def edit = {
        def appPlaceInstance = AppPlace.get(params.id)
        if (!appPlaceInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [appPlaceInstance: appPlaceInstance]
        }
    }

    def update = {
        def appPlaceInstance = AppPlace.get(params.id)
        if (appPlaceInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (appPlaceInstance.version > version) {
                    
                    appPlaceInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'appPlace.label', default: 'AppPlace')] as Object[], "Another user has updated this AppPlace while you were editing")
                    render(view: "edit", model: [appPlaceInstance: appPlaceInstance])
                    return
                }
            }
            appPlaceInstance.properties = params
            if (!appPlaceInstance.hasErrors() && appPlaceInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), appPlaceInstance.id])}"
                redirect(action: "show", id: appPlaceInstance.id)
            }
            else {
                render(view: "edit", model: [appPlaceInstance: appPlaceInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def appPlaceInstance = AppPlace.get(params.id)
        if (appPlaceInstance) {
            try {
                appPlaceInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'appPlace.label', default: 'AppPlace'), params.id])}"
            redirect(action: "list")
        }
    }
}
